package com.haoxin.ai_course.common.utils;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;

public class CountTool {
	public static Integer countMedianTime(List<Integer>list) {
		list = list.stream().sorted().collect(Collectors.toList());
		Integer time = 0;
		int size = list.size();
		if(CollectionUtils.isEmpty(list)) {
			return 0;
		}
		if(size == 1) {
			return list.get(0);
		}
		if(size %2 == 0) {
			int index1 = size/2-1;
			int index2 = index1+1;
			time = (list.get(index1)+list.get(index2))/2;
		}else {
			int index = size/2;
			time = list.get(index);
		}
		return time;
	}
	public static Integer countAvageTime(List<Integer>list) {
		list = list.stream().sorted().collect(Collectors.toList());
		int sumTime = list.stream().collect(Collectors.summingInt(Integer::intValue));
		int size = list.size();
		if(CollectionUtils.isEmpty(list)) {
			return 0;
		}
		if(size == 1) {
			return list.get(0);
		}
		return sumTime/size;
	}
	public static BigDecimal countSingleCourseFinishRate(int finishCount,long enterCount) {
		BigDecimal singleCourseFinishRate = new BigDecimal("100");
		if(enterCount == 0 ||finishCount == 0) {
			singleCourseFinishRate =  new BigDecimal("0").setScale(2);
		}else {
			singleCourseFinishRate = BigDecimal.valueOf(finishCount).divide(BigDecimal.valueOf(enterCount), 4, RoundingMode.HALF_UP)
					.multiply(BigDecimal.valueOf(100)).setScale(2);	
		}
		
		return singleCourseFinishRate;
	}

	public static void main(String[] args) {
//		74
//		84
//		127
//		135
		List<Integer> list = new ArrayList<Integer>();
		list.add(84);
		list.add(127);
		list.add(74);
		list.add(135);
		System.out.println(list);
		System.out.println(CountTool.countMedianTime(list));
		System.out.println(CountTool.countAvageTime(list));
		
		System.out.println(countSingleCourseFinishRate(1, 1));
	}
}
